import Quill from 'quill';

const Embed = Quill.import('blots/embed');

class AtomBlot extends Embed {
  static create ({_id, _name, _uid, _text}) {
    const node = super.create();
    node.setAttribute('_id', _id)
    node.setAttribute('_name', _name)
    node.setAttribute('_uid', _uid)
    node.setAttribute('data-blot', true)

    node.setAttribute('contenteditable', false)
    //node.appendChild(document.createTextNode(text))
    node.innerHTML = _text;
    return node
  }
  static formats (node) {
    const format = {}

    if (node.hasAttribute('_id')) {
      format._id = node.getAttribute('_id')
    }

    if (node.hasAttribute('_name')) {
      format._name = node.getAttribute('_name')
    }
    if (node.hasAttribute('_uid')) {
      format._uid = node.getAttribute('_uid')
    }
    if (node.innerHTML) {
      format._text = node.innerHTML;
    }
    
    return format
  }
  format (name, value) {
    if (name === '_id' || name === '_name' || name === '_uid') {
      if (value) {
        this.domNode.setAttribute(name, value)
      } else {
        this.domNode.removeAttribute(name, value)
      }
    } else {
      super.format(name, value)
    }
  }
  static value (node) {
    return {
      _id: node.getAttribute('_id'),
      _name: node.getAttribute('_name'),
      _uid: node.getAttribute('_uid'),
      _text: node.innerHTML
    }
  }
}
AtomBlot.blotName = 'Atom';
AtomBlot.tagName = 'strong';
AtomBlot.className = 'quill-atom';

class AtomParamBlot extends Embed {
  static create ({_id, _uid, _atomId, _name, _text}) {
    const node = super.create();
    node.setAttribute('_id', _id)
    node.setAttribute('_uid', _uid)
    node.setAttribute('_atomid', _atomId)
    node.setAttribute('_name', _name)
    node.setAttribute('data-blot', true)

    node.setAttribute('contenteditable', false)
    node.innerHTML = _text;
    return node
  }
  static formats (node) {
    const format = {}

    if (node.hasAttribute('_id')) {
      format._id = node.getAttribute('_id')
    }

    if (node.hasAttribute('_name')) {
      format._name = node.getAttribute('_name')
    }

    if (node.hasAttribute('_atomid')) {
      format._atomid = node.getAttribute('_atomid')
    }
    if (node.hasAttribute('_uid')) {
      format._uid = node.getAttribute('_uid')
    }
    if (node.innerHTML) {
      format._text = node.innerHTML;
    }
    return format
  }
  format (name, value) {
    if (name === '_id' || name === '_name' || name === '_atomid' || name === '_uid') {
      if (value) {
        this.domNode.setAttribute(name, value)
      } else {
        this.domNode.removeAttribute(name, value)
      }
    } else {
      super.format(name, value)
    }
  }
  static value (node) {
    return {
      _id: node.getAttribute('_id'),
      _name: node.getAttribute('_name'),
      _atomid: node.getAttribute('_atomid'),
      _uid: node.getAttribute('_uid'),
      _text: node.innerHTML
    }
  }
}
AtomParamBlot.blotName = 'AtomParam';
AtomParamBlot.tagName = 'strong';
AtomParamBlot.className = 'quill-atom-param';


class OperatorBlot extends Embed {
  static create ({_id, _uid, _tid, _name, _text}) {
    const node = super.create();
    node.setAttribute('_id', _id)
    node.setAttribute('_uid', _uid)
    node.setAttribute('_tid', _tid)
    node.setAttribute('_name', _name)
    node.setAttribute('data-blot', true)

    node.setAttribute('contenteditable', false)
    node.innerHTML = _text;
    return node
  }
  static formats (node) {
    const format = {}

    if (node.hasAttribute('_id')) {
      format._id = node.getAttribute('_id')
    }

    if (node.hasAttribute('_name')) {
      format._name = node.getAttribute('_name')
    }

    if (node.hasAttribute('_tid')) {
      format._atomid = node.getAttribute('_tid')
    }
    if (node.hasAttribute('_uid')) {
      format._uid = node.getAttribute('_uid')
    }
    if (node.innerHTML) {
      format._text = node.innerHTML;
    }
    return format
  }
  format (name, value) {
    if (name === '_id' || name === '_name' || name === '_tid' || name === '_uid') {
      if (value) {
        this.domNode.setAttribute(name, value)
      } else {
        this.domNode.removeAttribute(name, value)
      }
    } else {
      super.format(name, value)
    }
  }
  static value (node) {
    return {
      _id: node.getAttribute('_id'),
      _name: node.getAttribute('_name'),
      _tid: node.getAttribute('_tid'),
      _uid: node.getAttribute('_uid'),
      _text: node.innerHTML
    }
  }
}
OperatorBlot.blotName = 'Operator';
OperatorBlot.tagName = 'strong';
OperatorBlot.className = 'quill-atom-operator';

Quill.register('formats/Atom', AtomBlot, true)
Quill.register('formats/AtomParam', AtomParamBlot, true)
Quill.register('formats/Operator', OperatorBlot, true)
export default { AtomBlot, AtomParamBlot, OperatorBlot}